package com.cooper.controllers.admin;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.cooper.controllers.Controller;
import com.cooper.core.JsonResult;
import com.cooper.entity.PageDetailEntity;
import com.cooper.entity.PagePanelEntity;
import com.cooper.entity.UserInfo;
import com.cooper.service.admin.PageEditService;
import com.cooper.util.Controllers;
import com.cooper.util.StringUtils;
import com.cooper.vo.PageEditVo;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;

import net.paoding.rose.web.Invocation;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.portal.Portal;

public class PageEditController extends Controller {
	
	@Autowired
	private PageEditService pageEditService;
	
	@Get
	public String execute(Invocation inv, Portal portal) throws Exception{
		String pageKey = inv.getParameter("pageKey");
		if(StringUtils.isNotBlank(pageKey)){
			PagePanelEntity pagePanel = this.pageEditService.getPagePanelByKey(pageKey);
			inv.addModel("entity", pagePanel);
			if(null != pagePanel){
				if(StringUtils.isNotBlank(pagePanel.getCssUrls())){
					List<String> cssUrlList = Splitter.on(",").splitToList(pagePanel.getCssUrls());
					inv.addModel("cssUrlList", cssUrlList);
				}
			}
		}
		return "PageEdit";
	}
	
	@Post("save")
	public Object savePagePanel(Invocation inv, PagePanelEntity pagePanel) throws Exception{
		JsonResult json = new JsonResult();
		UserInfo userInfo = Controllers.getUserInfo(inv);
		PagePanelEntity entity = this.pageEditService.getPagePanelByKey(pagePanel.getPageKey());
		entity.setContent(pagePanel.getContent());
		Controllers.prepareEntityForDataSaving(entity, userInfo, true);
		this.pageEditService.updatePagePanel(entity);
		json.setSuccess(true);
		return json;
	}
	
	@Get("detail")
	public String detail(Invocation inv, PageEditVo editVo) throws Exception{
		String linkType = null;
		List<String> cssUrlList = Lists.newArrayList();
		
		if(!StringUtils.isEmpty(editVo.getDetailId())){
			PageDetailEntity pageDetail = this.pageEditService.getPageDetailById(editVo.getDetailId());
			inv.addModel("pageDetail", pageDetail);
			if(null != pageDetail){
				linkType = pageDetail.getLinkType();
				if(StringUtils.isNotBlank(pageDetail.getCssUrls())){
					List<String> cssUrlDetail = Splitter.on(",").splitToList(pageDetail.getCssUrls());
					cssUrlList.addAll(cssUrlDetail);
				}
			}
		}
		
		PagePanelEntity pagePanel = this.pageEditService.getPagePanelByKey(editVo.getPageKey());
		inv.addModel("pagePanel", pagePanel);
		if(StringUtils.isEmpty(linkType)){
			linkType = pagePanel.getLinkType();
		}
		inv.addModel("linkType", linkType);
		
		if(StringUtils.isNotBlank(pagePanel.getCssUrls())){
			List<String> cssUrlPanel = Splitter.on(",").splitToList(pagePanel.getCssUrls());
			cssUrlList.addAll(cssUrlPanel);
		}
		inv.addModel("cssUrlList", cssUrlList);
		return "PageDetail";
	}

	@Post("saveDetail")
	public Object saveDetail(Invocation inv, PageDetailEntity pageDetail) throws Exception{
		JsonResult json = new JsonResult();
		UserInfo userInfo = Controllers.getUserInfo(inv);
		boolean bUpdate = !StringUtils.isEmpty(pageDetail.getId());
		Controllers.prepareEntityForDataSaving(pageDetail, userInfo, bUpdate);
		
		Long detailId = pageDetail.getId();
		if(bUpdate){
			this.pageEditService.updatePageDetail(pageDetail);
		} else {
			detailId = this.pageEditService.savePageDetail(pageDetail);
		}
		json.setSuccess(true);
		json.put("detailId", detailId);
		return json;
	}
}

